﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace Project01
{
    //Dùng để gọi Monster ra
    public sealed class MonsterComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        
        //struct dùng để lấy các giá trị về các loại monster sẽ load lên trong khoảng time nào
        struct MT
        {
           public byte Type;
           public float Time;
           public byte PositionX;
        }
        //lấy các giá trị trong file txt sẽ dc load lên
        List<MT> ListLoadMonster = new List<MT>();
      
        //List Monster
        List<Monster> MonsterList;

        internal List<Monster> MList
        {
            get { return MonsterList; }
            set { MonsterList = value; }
        }


     

        Monster monster;

        SpriteBatch spriteBatch;

        StreamReader MonsterRead;
        //Thời gian monster xuất hiện
        TimeSpan TimeMonster;

        byte Couter=1;
        bool createboss=false;
        bool createMonster = false;
        public MonsterComponent(Game game)
            : base(game)
        {
        }
        private List<int> PositionX(Game game)
        {
            List<int> List = new List<int>();
            for (int i = 1; i <= game.Window.ClientBounds.Width / 100; i++)
            {
                List.Add(100 * i);
            }
            return List;
            
        }
        public override void Initialize()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            CreateMonster(Couter);
                base.Initialize();
        }
        #region CreateMonster
        private void CreateMonster(byte Couter)
        {
            MonsterList = new List<Monster>();
             MonsterRead = new StreamReader("Monster" + Couter + ".txt");
            using (MonsterRead)
            {
                while (MonsterRead.Peek() != -1)
                {
                    string doc = MonsterRead.ReadLine();
                    string[] Data = doc.Split('\t');
                    MT M = new MT();
                    M.Type = Convert.ToByte(Data[0]);
                    M.Time = float.Parse(Data[1]);
                    M.PositionX = Convert.ToByte(Data[2]);
                    ListLoadMonster.Add(M);
                }
            }
            MonsterRead.Close();
        }
        #endregion
        #region CreateBoss
        private void CreateBoss(byte Couter)
        {
            switch (Couter)
            {
                case 1:
                    {
                        monster = new Boss01(Game);
                        monster.Sprite.Position = SetPosition(PositionX(Game)[2], monster.Sprite.RecHeight, monster.Sprite.RecWidth);
                        break;
                    }
                case 2:
                    {
                        monster = new Boss02(Game);
                        monster.Sprite.Position = SetPosition(PositionX(Game)[2], monster.Sprite.RecHeight, monster.Sprite.RecWidth);
                        break;
                    }
            }
            MonsterList.Add(monster);
            createboss = false;
            createMonster = true;
            MonsterRead.Close();
        }
        #endregion
        protected override void LoadContent()
        {
           


                base.LoadContent();
        }
        private void  LoadContentMonster(GameTime gametime)
        {
            if (TimeMonster == TimeSpan.Zero && Couter>1)
                CreateMonster(Couter);
            TimeMonster += gametime.ElapsedGameTime;
           
            if (ListLoadMonster.Count == 0 && TimeMonster>=TimeSpan.FromSeconds(35))
                createboss = true;
            for (int i = 0; i < ListLoadMonster.Count;i++)
            {
                if (TimeMonster >= TimeSpan.FromSeconds(ListLoadMonster[0].Time))
                {
                    switch (ListLoadMonster[0].Type)
                    {
                        case 1:
                            monster = new MonsterType01(Game);
                            monster.Sprite.Position = SetPosition(PositionX(Game)[ListLoadMonster[0].PositionX], monster.Sprite.RecHeight, monster.Sprite.RecWidth);
                            break;
                        case 2:
                            monster = new MonsterType02(Game);
                            monster.Sprite.Position = SetPosition(PositionX(Game)[ListLoadMonster[0].PositionX], monster.Sprite.RecHeight, monster.Sprite.RecWidth);
                            break;
                        case 3:
                          
                            monster = new MonsterType03(Game);
                            monster.Sprite.Position = SetPosition(PositionX(Game)[ListLoadMonster[0].PositionX], monster.Sprite.RecHeight, monster.Sprite.RecWidth);
                          

                            break;
                        case 4:

                            monster = new MonsterType04(Game);
                            monster.Sprite.Position = SetPosition(PositionX(Game)[ListLoadMonster[0].PositionX], monster.Sprite.RecHeight, monster.Sprite.RecWidth);
                            
                            break;
                        case 5:

                            monster = new MonsterType05(Game);
                            monster.Sprite.Position = SetPosition(PositionX(Game)[ListLoadMonster[0].PositionX], monster.Sprite.RecHeight, monster.Sprite.RecWidth);
                            break;
                        case 6:
                            monster = new MonsterType06(Game);
                            monster.Sprite.Position = SetPosition(PositionX(Game)[ListLoadMonster[0].PositionX], monster.Sprite.RecHeight, monster.Sprite.RecWidth);
                            break;
                        case 249:
                            {
                                monster = new Boss01(Game);
                                monster.Sprite.Position = SetPosition(PositionX(Game)[ListLoadMonster[0].PositionX], monster.Sprite.RecHeight, monster.Sprite.RecWidth);
                                break;
                            }
                    }
                    MonsterList.Add(monster);
                    ListLoadMonster.RemoveAt(0);
                    break;
                }

                }
            
        }
        public override void Update(GameTime gameTime)
        {
            if (!createMonster)
            {
                LoadContentMonster(gameTime);
            }
            if (createboss)
            {
                CreateBoss(Couter);
            }
            for (int i = 0; i < MonsterList.Count; i++)
            {
                MonsterList[i].Update(gameTime);
                if (MonsterList[i].Type == 250 - Couter && MonsterList[i].Active)
                {
                    Couter++;
                    createMonster = false;
                    createboss = false;
                    TimeMonster = TimeSpan.Zero;
                }
                if (MonsterList[i].Active)
                {
                    MonsterList[i] = null;
                    MonsterList.RemoveAt(i);
                    i--;
                }
            }
           
            base.Update(gameTime);
        }


        private Vector2 SetPosition(int PositionX ,int recHeight,int RecWidth)
        {
            return new Vector2(PositionX - RecWidth, 0-recHeight);
        }

        #region TeamMoveofVertical
        private void TeamThangDungBenTrai(int Columns)
        {
            for (int i = 1; i <= Columns; i++)
            {

                Monster M = new MonsterType01(Game);
                M.Sprite.Position = new Vector2(50, (-M.Sprite.RecHeight - 50) * i);
                MonsterList.Add(M);

            }
        }

        private void TeamThangDungGiua(int Columns)
        {
            for (int i = 1; i <= Columns; i++)
            {
                Monster M = new MonsterType05(Game);
                M.Sprite.Position = new Vector2(Game.Window.ClientBounds.Width / 2 - M.Sprite.RecWidth, (-M.Sprite.RecHeight - 50) * i);
                M.LoadContent();
                MonsterList.Add(M);


            }
        }
        private void TeamThangDungBenPhai(int Columns)
        {
            for (int i = 1; i <= Columns; i++)
            {
                Monster M = new MonsterType05(Game);
                M.Sprite.Position = new Vector2(Game.Window.ClientBounds.Width - 100, (-M.Sprite.RecHeight - 50) * i);
                M.LoadContent();
                MonsterList.Add(M);
            }
        }
        #endregion
        private void TeamTamGiac(int rows, int columns)
        {
            //3,5
            for (int i = 1; i <= rows; i++)
                for (int j = 1; j <= columns; j++)
                {

                    if (j == i || j == columns - i + 1)
                    {
                        Monster M = new MonsterType05(Game);
                        M.Sprite.Position = new Vector2(150 * j, -Game.Window.ClientBounds.Height + (150 * i));
                        MonsterList.Add(M);
                    }

                }
        }
        public override void Draw(GameTime gameTime)
        {

            spriteBatch.Begin();
            for (int i = 0; i < MonsterList.Count; i++)
            {
                MonsterList[i].Draw(spriteBatch);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        } 
    }
}
